package Model.Matchmaking;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import DBAccess.DBAccess;

public class Match {
	private int userID;
	private int score;
	
	public Match(int userID, int score){
		this.userID = userID;
		this.score = score;
	}

	public int getUserID() {
		return userID;
	}
	public void setUserID(int userID) {
		this.userID = userID;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	
	public static ArrayList<Match> getMatches(int userID){
		ArrayList<Match> results = new ArrayList<Match>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			ps = DBAccess.con.prepareStatement("SELECT * FROM matchmakingdb WHERE userID=? OR otherID=? ORDER BY score DESC");
			ps.setInt(1, userID);
			ps.setInt(2, userID);
			rs = ps.executeQuery();
			for (int i=0; rs.next() && i<10; i++){
				int id1 = rs.getInt("userID");
				int id2 = rs.getInt("otherID");
				int otherID = id1==userID?id2:id1;
				int score = rs.getInt("score");
				Match match = new Match(otherID, score);
				results.add(match);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		DBAccess.close(ps, rs);
		return results;
	}
}
